package com.example.myview.coroutineText

import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.await
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

object ProjectSelectNetWork {
    private val selectService = ServiceCreator.create(ApiService::class.java)
    suspend fun select(query: Int) = selectService.getProjectSelect(query).await()

    suspend fun homeBean(query: Int) = selectService.getHomeBean(query).await()

    private suspend fun <T>Call<T>.await():T{
        return suspendCoroutine {
            enqueue(object :Callback<T>{
                override fun onResponse(call: Call<T>, response: Response<T>) {
                    var body = response.body()
                    if (body != null)
                        it.resume(body)
                    else
                        it.resumeWithException(RuntimeException("response body is null"))
                }
                override fun onFailure(call: Call<T>, t: Throwable) {
                }

            })
        }
    }


}